<script setup>
import draggable from "vuedraggable";
import NameIcon from '../../NameIcon.vue';

let props = defineProps({
  control: Object,
  // formProps: Object,
  enData: Object
})
// const control = defineModel("control")
// defineProps({
//   enData: Object,
// })

console.log(props.control.props.options[0])
console.log('endata', props.enData)
function requiredChange(value) {
  props.control.rules[0].required = value;
}

function requiredMessageChange(value) {
  props.control.rules[0].message = value;
}

function addOption() {
  props.control.props.options.push({ label: '', value: '' });
}

function removeOption(index) {
  props.control.props.options.splice(index, 1)
}
</script>

<template>
  <el-form label-width="90px">
    <el-form-item label="中文名称">
      <el-input v-model="props.control.props.label" placeholder="请输入中文名称"></el-input>
    </el-form-item>
    <el-form-item label="英文名称">
      <el-select v-if="props.enData?.options?.length" v-model="props.control.props.enName" placeholder="请选择英文名称"
        style="width: 100%">
        <el-option v-for="(item, index) in props.enData.options" :key="index" :label="item[props.enData.label]"
          :value="item[props.enData.value]" />
      </el-select>
      <el-input v-else v-model="props.control.props.enName" placeholder="请输入英文名称"></el-input>
    </el-form-item>
    <el-form-item label="填写提示">
      <el-input v-model="props.control.props.placeholder" placeholder="请选择"></el-input>
    </el-form-item>
    <el-form-item label="默认值">
      <el-select v-model="props.control.props.defaultValue" clearable placeholder="请输入默认值" style="width: 100%">
        <el-option v-for="(item, index) in props.control.props.options" :key="index"
          :label="props.control.props.showOptionLabel ? item.label : item.value" :value="item.value"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="匹配字典">
      <el-select v-model="props.control.props.dictionary" placeholder="请选择匹配字典" style="width: 100%">
        <el-option label="是" value="1" />
        <el-option label="否" value="0" />
      </el-select>
    </el-form-item>
    <el-form-item label="描述信息">
      <el-input type="textarea" rows="5" v-model="props.control.props.remark" placeholder="请输入描述信息"></el-input>
    </el-form-item>
    <el-form-item label="是否必填">
      <el-switch @change="requiredChange" v-model="props.control.props.required"></el-switch>
    </el-form-item>
    <el-form-item label="必填提示" v-if="props.control.props.required">
      <el-input @change="requiredMessageChange" v-model="props.control.props.requiredMessage"></el-input>
    </el-form-item>

    <el-form-item label="是否禁用">
      <el-switch v-model="props.control.props.disabled"></el-switch>
    </el-form-item>
    <el-form-item label="是否可筛选">
      <el-switch v-model="props.control.props.filterable"></el-switch>
    </el-form-item>
    <el-divider>选项</el-divider>
    <el-form-item label="自定义文本" class="mb-0 -mt-2">
      <el-switch v-model="props.control.props.showOptionLabel"></el-switch>
    </el-form-item>
    <el-form-item label-width="0">
      <draggable class="space-y-1 w-full" handle=".cursor-move" :list="props.control.props.options" item-key="index"
        tag="div">
        <template #item="{ element, index }">
          <div class="flex items-center">
            <el-icon class="text-lg cursor-move">
              <NameIcon name="move" />
            </el-icon>
            <el-input class="flex-grow" placeholder="选项值" v-model="element.value"></el-input>
            <el-input class="flex-grow" v-if="props.control.props.showOptionLabel" placeholder="显示文本"
              v-model="element.label"></el-input>
            <el-icon class="text-lg cursor-pointer text-red-500" @click="removeOption(index)">
              <NameIcon name="delete" />
            </el-icon>
          </div>
        </template>
      </draggable>
      <el-button text type="primary" @click="addOption">
        <el-icon>
          <NameIcon name="plus" />
        </el-icon>
        添加
      </el-button>
    </el-form-item>
  </el-form>
</template>
